package org.compiere.model;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class MBCSFRInputForm extends X_BCS_FRInputForm {

	private static CLogger s_log = CLogger.getCLogger(MBCSFRInputForm.class);
	private String name = "";
	private int code = 0;
	private boolean isIntercompanySales = false;
	
	
	
	public boolean isIntercompanySales() {
		return isIntercompanySales;
	}

	public void setIntercompanySales(boolean isIntercompanySales) {
		this.isIntercompanySales = isIntercompanySales;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public MBCSFRInputForm(Properties ctx, int MBCSFRInputForm_ID,
			String trxName) {
		super(ctx, MBCSFRInputForm_ID, trxName);
		// TODO Auto-generated constructor stub
	}
	
	public MBCSFRInputForm(Properties ctx, ResultSet rs, String trxName){
		super(ctx, rs, trxName);
	}


	protected boolean afterSave(boolean newRecord, boolean success) {

		boolean is = false;

		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";

		// check existing lines
		sql = " SELECT * FROM BCS_FRInputFormLine \n "
				+ " WHERE BCS_FRInputForm_ID =" + get_ID();

		try {
			pstmt = DB.prepareStatement(sql, null);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				is = true;
			}
		} catch (SQLException e) {
			s_log.log(Level.SEVERE, "BCS_FRInputForm", e);
		} finally {
			DB.close(rs, pstmt);
			rs = null;
			pstmt = null;
		}

		if (is)
			; // if there are record then do nothing
		else { // else create position from setupina

			try {
				
			class ext{
				Integer dict = 0;
				Integer seqno = 0;
				Integer C_BPartner_ID = null;
			}
				
			List<ext> listOfColumns = new ArrayList<ext>();	
//			sql = " 	select l.BCS_FRColumnsDict_ID	\n" 
//					+"	from adempiere.BCS_FRInputform i	\n" 
//					+"	inner join adempiere.bcs_frsetupinputformcolumn l on l.BCS_FRSetupInputForm_ID = i.BCS_FRSetupInputForm_ID	\n" 
//					+"	where BCS_FRInputform_ID = " + get_ID();
//			sql = "select c.BCS_FRColumnsDict_ID	\n"
//					+"	from adempiere.BCS_FRSetupInputForm i	\n"
//					+"	inner join adempiere.bcs_frsetupinputformcolumn c on c.BCS_FRSetupInputForm_ID = i.BCS_FRSetupInputForm_ID	\n"	
//					+"	where i.BCS_FRSetupInputForm_ID = " + getBCS_FRSetupInputForm_ID();
			sql = "select c.BCS_FRColumnsDict_ID, coalesce(d.seqno,0), C_BPartner_ID	\n"
					+"	from adempiere.BCS_FRSetupInputForm i	\n"
					+"	inner join adempiere.bcs_frsetupinputformcolumn c on c.BCS_FRSetupInputForm_ID = i.BCS_FRSetupInputForm_ID	\n"	
					+"	left join adempiere.ad_treenodebcsfrcolumn d on d.node_id = c.bcs_frcolumnsdict_id	\n" 
					+"	where i.BCS_FRSetupInputForm_ID = " + getBCS_FRSetupInputForm_ID();
			
					pstmt = DB.prepareStatement(sql, null);
					rs = pstmt.executeQuery();
					while (rs.next()) {
						ext q = new ext();
						q.dict = rs.getInt(1);
						q.seqno = rs.getInt(2);
						q.C_BPartner_ID = rs.getInt(3);
						listOfColumns.add(q);
					}

			
			sql = " 	select l.BCS_FRPositionsDict_ID \n"
					+ "	from adempiere.BCS_FRSetupInputForm i \n"
					+ "	inner join adempiere.bcs_frsetupinputformline l on l.BCS_FRSetupInputForm_ID = i.BCS_FRSetupInputForm_ID \n"
					+ "	where i.BCS_FRSetupInputForm_ID = " + getBCS_FRSetupInputForm_ID()	+	"	\n" 
					+ " order by l.bcs_frsetupinputformline_id ";

				String Trx = get_TrxName();
				pstmt = DB.prepareStatement(sql, null);
				rs = pstmt.executeQuery();
				while (rs.next()) {
					MBCSFRInputFormLine line = new MBCSFRInputFormLine(getCtx(), 0, null);//get_TrxName());
					
					line.setBCS_FRPositionsDict_ID(rs.getInt(1));
					line.setBCS_FRInputForm_ID(get_ID());
					line.save(null);
					if(line.get_ID() > 0){
						for(ext column: listOfColumns){
							
							String sql2 = 
									"INSERT INTO adempiere.bcs_frinputformcolumn(	\n"	+
											"ad_client_id, " +
											"ad_org_id, " +
											"bcs_frinputformcolumn_id, " +
											"created, " +
											"createdby,	\n" + 
											"isactive, " +
											"updated, " +
											"updatedby, " +
											"value1, " +
											"bcs_frinputformline_id,	\n"	+ 
											"bcs_frcolumnsdict_id, " +
											"seqno," +
											"c_bpartner_id" +
										")	\n"	+
										"VALUES (	\n"	+
										Env.getAD_Client_ID(Env.getCtx()) + ",	\n"	+ 
										Env.getAD_Org_ID(Env.getCtx()) + ",	\n"	+  
										"(select coalesce(max(bcs_frinputformcolumn_id)+1,1000000) from adempiere.bcs_frinputformcolumn),	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'Y',	\n"	+ 
										"'" + new Timestamp(System.currentTimeMillis()) +  "',	\n"	+ 
										Env.getAD_User_ID(Env.getCtx())+",	\n"	+ 
										"'',	\n"	+ 
										line.get_ID() + ",	\n"	+ 
										column.dict + ", 	\n"	+
										column.seqno + ",	\n"	+
										column.C_BPartner_ID + ")	\n"	;
										
							DB.executeUpdate(sql2);
							
//						"returning bcs_frinputformcolumn_id	\n";
							
//							PreparedStatement pstmt2 = null;
//							ResultSet rs2 = null;
//
//							pstmt2 = DB.prepareStatement(sql2, null);
//							rs2 = pstmt2.executeQuery();

							
							
//							MBCSFRInputFormColumn Mcolumn = new MBCSFRInputFormColumn(getCtx(), 0, null);
//							Mcolumn.setBCS_FRInputFormLine_ID(line.get_ID());
//							Mcolumn.setBCS_FRColumnsDict_ID(column.i1);
//							Mcolumn.setSeqNo(column.i2);
////							Mcolumn.setValue(new BigDecimal(0));
//							Mcolumn.save(null);
						}
					}

				}
			} catch (SQLException e) {
				s_log.log(Level.SEVERE, "MBCSFRInputForm", e);
			} finally {
				DB.close(rs, pstmt);
				rs = null;
				pstmt = null;
			}
		}

		return true;
	}

	protected boolean afterDelete(boolean success){
		
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";	

		//delete companies
		sql = " SELECT BCS_FRInputFormLine_ID FROM BCS_FRInputFormLine \n " +
				   " WHERE BCS_FRInputform_ID ="  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				while(rs.next()){
					
					DB.executeUpdate("delete BCS_FRInputFormLine WHERE BCS_FRInputFormLine_ID ="  +  rs.getInt(1));
					DB.executeUpdate("delete BCS_FRInputFormColumn WHERE BCS_FRInputFormLine_ID ="  +  rs.getInt(1));
//					MBCSFRInputFormLine line = new MBCSFRInputFormLine(Env.getCtx(), rs.getInt(1), null);
//					if(line != null)
//						line.delete(true);
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSFRInputForm", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		return true;
	}

	
}
